Public Partial Class WebForm1
    Inherits System.Web.UI.Page
    Private Banco As CapaDeNegocios.nBanco = New CapaDeNegocios.nBanco
    Private Cuenta As CapaDeNegocios.nCuenta = New CapaDeNegocios.nCuenta
    Private ChequeTerceros As CapaDeNegocios.nChequesTerceros = New CapaDeNegocios.nChequesTerceros
    '    Private ImputacionCheques As CapaDeNegocios.nFacCheque = New CapaDeNegocios.nFacCheque
    Dim DtsImputaciones As DataSet
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Session("Login") = "Anonimo" Then
            Response.Redirect("/")
        End If

        If Not Page.IsPostBack Then
            DtsImputaciones = New DataSet()
            DtsImputaciones.Tables.Add("TblImputaciones")
            DtsImputaciones.Tables(0).Columns.Add("Nombre")
            DtsImputaciones.Tables(0).Columns.Add("Total")
            DtsImputaciones.Tables(0).Columns.Add("Banco")
            DtsImputaciones.Tables(0).Columns.Add("Cuenta")
            DtsImputaciones.Tables(0).Columns.Add("FechaVence")
            DtsImputaciones.Tables(0).Columns.Add("FechaEmision")
            DtsImputaciones.Tables(0).Columns.Add("Observacion")
            DtsImputaciones.Tables(0).Columns.Add("ANombreDe")
            DtsImputaciones.Tables(0).Columns.Add("IdCheque")
            DtsImputaciones.Tables(0).Columns.Add("NumeroRetencion")
            DtsImputaciones.Tables(0).Columns.Add("DescripcionRetencion")
            Session("DtsImputaciones") = DtsImputaciones
            LlenarCboCheque()
            LlenarCboBanco()
            '            TxtTotal.te
            LimpiarCampos()
            LblTotal.Text = Request.QueryString("Importe")
            'LblTotal.Text = "23,42"
            'RVTotal.MaximumValue = LblTotal.Text
            'RVTotal.ErrorMessage = "Error, El valor debe ser mayor a 0 y menor o igual a " + LblTotal.Text
            LnkAceptar.OnClientClick = "javascript:window.close();"
        End If
    End Sub
    Sub LlenarCboBanco()
        CboBanco.DataSource = Banco.ListarTodos(0, "", "")
        CboBanco.DataTextField = "Denominacion"
        CboBanco.DataValueField = "CodigoBanco"
        CboBanco.DataBind()
        RdbBanco.Enabled = CboBanco.Items.Count > 0
        If CboBanco.Items.Count > 0 Then
            LlenarCboCuenta()
        End If
    End Sub

    Sub LlenarCboCheque()
        CboCheque.DataSource = ChequeTerceros.BuscarCheque("", "", "", "", "", "", "A Cobrar", "")
        CboCheque.DataTextField = "IdCheque"
        CboCheque.DataValueField = "IdCheque"
        CboCheque.DataBind()
        RdbCheque.Enabled = CboCheque.Items.Count > 0
        If RdbCheque.Enabled Then
            CboCheque_SelectedIndexChanged(New System.Object, New System.EventArgs)
        End If
    End Sub

    Sub LlenarCboCuenta()
        CboCuenta.DataSource = Cuenta.ListarCuenta(0, "", "", CboBanco.SelectedValue, "")
        CboCuenta.DataTextField = "NumeroCuenta"
        CboCuenta.DataValueField = "CodigoCuenta"
        CboCuenta.DataBind()
        LnkAgregar.Visible = CboCuenta.Items.Count > 0
        CboCuenta.Enabled = LnkAgregar.Visible
    End Sub

    Sub LimpiarCampos()
        RdbOtra.Checked = True
        TxtTotal.Text = "0,00"
        TxtTotal.Enabled = Not RdbCheque.Checked
        PnlRetenciones.Visible = False
        RdbOtra_CheckedChanged(New System.Object, New System.EventArgs)
    End Sub
    Protected Sub RdbOtra_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RdbOtra.CheckedChanged
        PnlOtra.Visible = True
        PnlBanco.Visible = False
        PnlCheque.Visible = False
        LblImputacion.Text = "Otras :"
        TxtTotal.Enabled = Not RdbCheque.Checked
        TxtTotal.Text = "0,00"
        TxtNumero.Text = ""
        LnkAgregar.Visible = True
    End Sub

    Protected Sub RdbBanco_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RdbBanco.CheckedChanged
        PnlOtra.Visible = False
        PnlBanco.Visible = True
        PnlCheque.Visible = False
        LblImputacion.Text = "Banco :"
        TxtTotal.Enabled = Not RdbCheque.Checked
        TxtTotal.Text = "0,00"
        LnkAgregar.Visible = CboBanco.Items.Count > 0
        If LnkAgregar.Visible Then
            LlenarCboCuenta()
        End If
        'LlenarCboBanco()
    End Sub

    Protected Sub RdbCheque_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RdbCheque.CheckedChanged
        PnlOtra.Visible = False
        PnlBanco.Visible = False
        PnlCheque.Visible = True
        TxtTotal.Enabled = Not RdbCheque.Checked
        LblImputacion.Text = "Cheque :"
        '        LnkAgregar.Visible = CboCheque.Items.Count > 0
        '        If LnkAgregar.Visible Then
        CboCheque_SelectedIndexChanged(New System.Object, New System.EventArgs)
        'End If

    End Sub

    Sub VerificarExistenciaCheques()
        Dim i As Integer
        CboCheque.Enabled = False
        For i = 0 To CboCheque.Items.Count() - 1
            CboCheque.Enabled = CboCheque.Enabled Or CboCheque.Items(i).Enabled
        Next
        RdbCheque.Enabled = CboCheque.Enabled
        If Not RdbCheque.Enabled Then
            RdbCheque.Checked = False
            RdbOtra.Checked = True
            RdbOtra_CheckedChanged(New System.Object, New System.EventArgs)
        End If
    End Sub

    Protected Sub LnkAgregar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LnkAgregar.Click

        If ((CDbl(TxtTotal.Text) <= CDbl(LblTotal.Text) And CDbl(TxtTotal.Text) > 0)) Or ((CDbl(TxtTotal.Text) <= CDbl(LblTotal.Text) And CDbl(TxtTotal.Text) > 0) And RdbOtra.Checked And TxtNumero.Text <> "") Then
            LblTotal.Text = (CDbl(LblTotal.Text) - CDbl(TxtTotal.Text)).ToString()
            If LblTotal.Text.IndexOf(",") <> -1 Then
                If (LblTotal.Text.Length - LblTotal.Text.IndexOf(",") = 2) Then
                    LblTotal.Text = LblTotal.Text + "0"
                End If
            Else
                LblTotal.Text = LblTotal.Text + ",00"
            End If
            Dim dr As DataRow
            DtsImputaciones = Session("DtsImputaciones")
            dr = DtsImputaciones.Tables(0).NewRow()
            If RdbOtra.Checked Then
                dr("Nombre") = CboOtras.Text
                dr("Total") = TxtTotal.Text
                If CboOtras.Text = "Retenciones" Then
                    dr("NumeroRetencion") = TxtNumero.Text
                    dr("DescripcionRetencion") = CboDescripcion.Text
                End If
            End If
            If RdbCheque.Checked Then
                Dim DtsAux As DataSet = ChequeTerceros.BuscarCheque(CboCheque.SelectedValue.ToString(), "", "", "", "", "", "A Cobrar", "")
                dr("Nombre") = "Cheque : " + CboCheque.Text + " - " + DtsAux.Tables(0).Rows(0)("Banco").ToString()
                dr("Total") = TxtTotal.Text
                dr("FechaVence") = DtsAux.Tables(0).Rows(0)("FechaVence").ToString()
                dr("FechaEmision") = DtsAux.Tables(0).Rows(0)("FechaEmision").ToString()
                dr("Observacion") = DtsAux.Tables(0).Rows(0)("Observacion").ToString()
                dr("ANombreDe") = DtsAux.Tables(0).Rows(0)("ANombreDe").ToString()
                dr("IdCheque") = DtsAux.Tables(0).Rows(0)("IdCheque").ToString()
                CboCheque.Items(CboCheque.Items.IndexOf(CboCheque.Items.FindByValue(CboCheque.SelectedValue.ToString()))).Enabled = False
                VerificarExistenciaCheques()
                If RdbCheque.Enabled Then
                    Dim i As Integer
                    i = 0
                    Do While (i < CboCheque.Items.Count() - 1 And Not CboCheque.Items(i).Enabled)
                        i = i + 1
                    Loop
                    CboCheque.SelectedIndex = i
                    RdbCheque_CheckedChanged(New System.Object, New System.EventArgs)
                End If
            Else
                dr("FechaVence") = ""
                dr("FechaEmision") = ""
                dr("Observacion") = ""
                dr("ANombreDe") = ""
                dr("IdCheque") = ""
            End If
            If RdbBanco.Checked Then
                dr("Nombre") = CboBanco.SelectedItem.Text.ToString() + " - " + CboCuenta.SelectedItem.Text.ToString()
                dr("Total") = TxtTotal.Text
                dr("Banco") = CboBanco.SelectedValue.ToString()
                dr("Cuenta") = CboCuenta.SelectedValue.ToString()
            Else
                dr("Banco") = ""
                dr("Cuenta") = ""
            End If
            DtsImputaciones.Tables(0).Rows.Add(dr)

            Session("DtsImputaciones") = DtsImputaciones
            GridImputaciones.DataSource = DtsImputaciones
            GridImputaciones.DataBind()
            TxtTotal.Enabled = Not RdbCheque.Checked
            LblError.Text = ""
        Else
            If CDbl(TxtTotal.Text) = 0 Then
                LblError.Text = "Error, El valor ingresado no puede ser 0."
            End If
            If (CDbl(TxtTotal.Text) > CDbl(LblTotal.Text)) Then
                LblError.Text = "Error, El valor ingresado no puede ser mayor a " + LblTotal.Text
            End If
        End If

    End Sub

    Protected Sub CboBanco_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CboBanco.SelectedIndexChanged
        LlenarCboCuenta()
    End Sub

    Protected Sub GridImputaciones_RowCommand(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridImputaciones.RowCommand
        If e.CommandName = "Remover" Then
            DtsImputaciones = Session("DtsImputaciones")
            If DtsImputaciones.Tables(0).Rows(CInt(e.CommandSource.parent.parent.RowIndex))("IdCheque").ToString() <> "" Then
                CboCheque.Items(CboCheque.Items.IndexOf(CboCheque.Items.FindByValue(DtsImputaciones.Tables(0).Rows(CInt(e.CommandSource.parent.parent.RowIndex))("IdCheque").ToString()))).Enabled = True
                VerificarExistenciaCheques()
                If RdbCheque.Enabled Then
                    Dim i As Integer
                    i = 0
                    Do While (i < CboCheque.Items.Count() - 1 And Not CboCheque.Items(i).Enabled)
                        i = i + 1
                    Loop
                    CboCheque.SelectedIndex = i
                    '                    RdbCheque.Checked = True
                    '                    RdbCheque_CheckedChanged(New System.Object, New System.EventArgs)
                End If
            End If
            LblTotal.Text = (CDbl(LblTotal.Text) + CDbl(DtsImputaciones.Tables(0).Rows(CInt(e.CommandSource.parent.parent.RowIndex))("Total").ToString())).ToString()
            If LblTotal.Text.IndexOf(",") <> -1 Then
                If (LblTotal.Text.Length - LblTotal.Text.IndexOf(",") = 2) Then
                    LblTotal.Text = LblTotal.Text + "0"
                End If
            Else
                LblTotal.Text = LblTotal.Text + ",00"
            End If
            'RVTotal.ErrorMessage = "Error, El valor debe ser mayor a 0 y menor o igual a " + LblTotal.Text
            DtsImputaciones.Tables(0).Rows.Remove(DtsImputaciones.Tables(0).Rows(CInt(e.CommandSource.parent.parent.RowIndex)))
            GridImputaciones.DataSource = DtsImputaciones
            GridImputaciones.DataBind()
            TxtTotal.Enabled = Not RdbCheque.Checked
            LblError.Text = ""
        End If
    End Sub

    Protected Sub CboCheque_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CboCheque.SelectedIndexChanged
        TxtTotal.Text = ChequeTerceros.BuscarCheque(CboCheque.SelectedValue.ToString(), "", "", "", "", "", "A Cobrar", "").Tables(0).Rows(0)("Importe").ToString()
        'CboCheque.Items(CboCheque.SelectedIndex).Value.ToString(), "", "", "", "", "", "A Cobrar", "").Tables(0).Rows(0)("Importe").ToString()
    End Sub

    Protected Sub GridImputaciones_RowDataBound(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridImputaciones.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim IntLnkMasInformacion As LinkButton = e.Row.FindControl("LnkMasInformacion")
            Dim IntLblSeparador As Label = e.Row.FindControl("LblSeparador")
            Dim IntHFBanco As HiddenField = e.Row.FindControl("HFBanco")
            Dim IntHFCuenta As HiddenField = e.Row.FindControl("HFCuenta")
            Dim IntHFIdCheque As HiddenField = e.Row.FindControl("HFIdCheque")
            IntLnkMasInformacion.Visible = IntHFBanco.Value <> "" Or IntHFIdCheque.Value <> ""
            IntLblSeparador.Visible = IntLnkMasInformacion.Visible
            If IntLnkMasInformacion.Visible Then
                IntLnkMasInformacion.OnClientClick = "javascript:var popup = window.open('FrmInformacionDeImputaciones.aspx?IdCheque=" + IntHFIdCheque.Value.ToString() + "&IdBanco=" + IntHFBanco.Value.ToString() + "&IdCuenta=" + IntHFCuenta.Value.ToString() + "',(new Date().getTime()),'Width=660,Height=500,Scrollbars=NO,Resizable=NO,Location=NO,Menubar=NO,Status=NO,Titlebar=NO,Toolbar=NO')"
            End If
        End If
    End Sub

    Protected Sub CboOtras_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CboOtras.SelectedIndexChanged
        PnlRetenciones.Visible = CboOtras.Text = "Retenciones"
        If PnlRetenciones.Visible Then
            CboDescripcion.SelectedIndex = 0
            TxtNumero.Text = ""
        End If

    End Sub
End Class